// rsa加密解密
import JSEncrypt from 'jsencrypt';
// base64编码和解码
import {Base64} from 'js-base64'
// aes加密解密
import CryptoJS from 'crypto-js';
import store from "../store"


// rsa加密
function jiamiRSA(text){
    let encryptStr = new JSEncrypt();
    const pubKey = store.state.encryption.publickey
    console.log(pubKey,"公钥")
    encryptStr.setPublicKey(pubKey); // 设置 加密公钥
    const jieguo = encryptStr.encrypt(text.toString());
    console.log(jieguo,"rsa加密结果")
    return jieguo
}
// rsa解密
function jiemiRSA(text){
    let encryptStr = new JSEncrypt();
    const siyao = ""
    encryptStr.setPrivateKey(siyao)//解密公钥
    return encryptStr.decrypt(text.toString()) //解密
}

// rsa分段加密

// rsa分段解密

// aes加密
function jiamiAES(text){
    const aeskey = store.state.encryption.aeskey
    const key = CryptoJS.enc.Utf8.parse(aeskey);
    const srcs = CryptoJS.enc.Utf8.parse(text);
    const jieguo = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7 }).toString();
    console.log(jieguo,"加密的结果")
    return jieguo
}
// aes解密
function jiemiAES(text){
    console.log(text,"被加密的内容")
    console.log(typeof text,"被加密的内容")
    const aeskey = store.state.encryption.aeskey
    console.log(aeskey,"aes密钥")
    let key = CryptoJS.enc.Utf8.parse(aeskey);
    let decrypt = CryptoJS.AES.decrypt(text, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

// base64编码
function bianma(text){
    const  jieguo  =Base64.encode(text.toString());
    console.log(jieguo,"编码结果")
    return jieguo
}
// base64解码
function jiema(text){
    return Base64.decode(text.toString());
}

function sendjiami(text){
    return jiamiRSA(text)
}

function getjiemi(text){
    return jiemiAES(text)
}

function ceshi(text){
    return jiamiAES(text)
}

export default {sendjiami , getjiemi,ceshi}